package ogdkp.dialog;

import java.util.List;

import ogdkp.om.Player;
import ogdkp.providers.DKPContentProvider;
import ogdkp.providers.DKPLabelProvider;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

public class SetRaidersDialog extends TitleAreaDialog {

	List<Player> players;

	private TableViewer viewer;

	public SetRaidersDialog(final Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void create() {
		super.create();
		setTitle("Set Raiders");
		setMessage("Select the Raiders for tonight.");
	}

	@Override
	protected Control createDialogArea(final Composite parent) {
		final Composite container = new Composite(parent, SWT.NONE);

		final GridLayout layout = new GridLayout();
		container.setLayout(layout);

		final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.widthHint = 200;
		data.heightHint = 400;
		container.setLayoutData(data);

		viewer = new TableViewer(container, SWT.CHECK | SWT.V_SCROLL);
		viewer.setContentProvider(new DKPContentProvider());
		viewer.setLabelProvider(new DKPLabelProvider());
		viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		if (players != null) {
			viewer.setInput(players.toArray());
			for (final TableItem item : viewer.getTable().getItems()) {
				item.setChecked(((Player) item.getData()).isRaiding());
			}
		}

		final Composite buttonPane = new Composite(container, SWT.NONE);
		final GridLayout btnlayout = new GridLayout();
		btnlayout.numColumns = 2;
		buttonPane.setLayout(btnlayout);

		final Button select = new Button(buttonPane, SWT.NONE);
		select.setText("Select All");
		select.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				for (final TableItem item : viewer.getTable().getItems()) {
					item.setChecked(true);
				}
			}
		});

		final Button deselect = new Button(buttonPane, SWT.NONE);
		deselect.setText("Deselect All");
		deselect.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				for (final TableItem item : viewer.getTable().getItems()) {
					item.setChecked(false);
				}
			}
		});

		return container;
	}

	public void setPlayers(final List<Player> players) {
		this.players = players;
	}

	@Override
	public void okPressed() {
		if (players != null) {
			for (final TableItem item : viewer.getTable().getItems()) {
				((Player) item.getData()).setRaiding(item.getChecked());
			}
		}
		super.okPressed();
	}
}
